import type { ConsultRecord } from '@/types/consult'

const STORAGE_KEY = 'consult_records'

// 生成模拟数据
const generateMockData = (): ConsultRecord[] => {
  return [
    {
      id: '1',
      elderName: '王文凤',
      gender: '女',
      age: 88,
      consultantName: '王晓鹏',
      relationship: '女儿',
      phone: '13866668888',
      intentLevel: '仅咨询',
      intentDescription: '咨询机构情况',
      status: '跟进中',
      nextFollowDate: '2025-02-20',
      followPerson: '王接待',
      followNote: '约定来院参观',
      firstConsultTime: '2025-02-07',
      firstReception: '王接待',
    },
    {
      id: '2',
      elderName: '李大爷',
      gender: '男',
      age: 75,
      consultantName: '李明',
      relationship: '儿子',
      phone: '13899999999',
      intentLevel: '强烈入住意向',
      intentDescription: '希望近期入住',
      status: '跟进中',
      nextFollowDate: '2025-02-15',
      followPerson: '张接待',
      followNote: '已预约实地考察',
      firstConsultTime: '2025-02-01',
      firstReception: '张接待',
    },
  ]
}

// 初始化数据
const initializeData = () => {
  const existingData = localStorage.getItem(STORAGE_KEY)
  if (!existingData) {
    const mockData = generateMockData()
    localStorage.setItem(STORAGE_KEY, JSON.stringify(mockData))
  }
}

// 获取所有记录
export const getAllRecords = () => {
  initializeData()
  const data = localStorage.getItem(STORAGE_KEY)
  return data ? JSON.parse(data) : []
}

// 分页获取记录
export const getRecords = (page: number, pageSize: number, query: string = '') => {
  const allRecords = getAllRecords()
  const filteredRecords = query
    ? allRecords.filter((record: ConsultRecord) => {
        const searchStr = query.toLowerCase()
        return (
          record.elderName.toLowerCase().includes(searchStr) ||
          record.consultantName.toLowerCase().includes(searchStr) ||
          record.phone.includes(searchStr)
        )
      })
    : allRecords

  const total = filteredRecords.length
  const start = (page - 1) * pageSize
  const end = start + pageSize
  const records = filteredRecords.slice(start, end)

  return {
    records,
    total,
  }
}

// 添加记录
export const addRecord = (record: Omit<ConsultRecord, 'id'>) => {
  const records = getAllRecords()
  const newRecord = {
    ...record,
    id: Date.now().toString(), // 使用时间戳作为临时ID
  }
  records.unshift(newRecord)
  localStorage.setItem(STORAGE_KEY, JSON.stringify(records))
  return newRecord
}

// 更新记录
export const updateRecord = (record: ConsultRecord) => {
  const records = getAllRecords()
  const index = records.findIndex((r: ConsultRecord) => r.id === record.id)
  if (index > -1) {
    records[index] = record
    localStorage.setItem(STORAGE_KEY, JSON.stringify(records))
    return true
  }
  return false
}

// 删除记录
export const deleteRecord = (id: string) => {
  const records = getAllRecords()
  const index = records.findIndex((r: ConsultRecord) => r.id === id)
  if (index > -1) {
    records.splice(index, 1)
    localStorage.setItem(STORAGE_KEY, JSON.stringify(records))
    return true
  }
  return false
}

// 获取单条记录
export const getRecordById = (id: string) => {
  const records = getAllRecords()
  return records.find((r: ConsultRecord) => r.id === id)
}
